﻿using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Threading;
using AutoMapper;
using MyCompanyName.AbpZeroTemplate.IRepositorys;
using MyCompanyName.AbpZeroTemplate.SystemSetting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.SystemSetting
{
    public class SystemSettingService : AbpZeroTemplateAppServiceBase, ISystemSettingService
    {
        private readonly ISystemSettingRepository _systemSettingRepository;
        public SystemSettingService(ISystemSettingRepository systemSettingRepository)
        {
            this._systemSettingRepository = systemSettingRepository;
        }


        /// <summary>
        /// 获得代理商列表
        /// </summary>
        /// <returns></returns>
       public PagedResultDto<SystemSettingDto> GetAgentDtoList(string SystemSettingName, int PageSize, int PageNumber)
        {
            var obj = _systemSettingRepository.GetAll();
            
            //判断逻辑

            //获取总数
            var dataCount = obj.Count();

            //默认的分页方式
            var dataList = obj.Where(m => m.Parametername.Contains(SystemSettingName))
                              .OrderByDescending(t => t.CreationTime)
                              .Skip(PageSize * (PageNumber - 1)).Take(PageSize).ToList();
            return new PagedResultDto<SystemSettingDto>(dataCount, dataList.MapTo<List<SystemSettingDto>>());
        }

        /// <summary>
        /// 创建系统设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int CreateSystemSetting(CreateSystemSettingInput input)
        {
            int result = 0;

            var task = Mapper.Map<SystemSetting>(input);
            task.CreationTime = DateTime.Now;
            task.R_Id = Guid.NewGuid();

            result = _systemSettingRepository.InsertAndGetId(task);


            return result;
        }

        /// <summary>
        /// 更新系统设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool UpdateSystemSetting(UpdateSystemSettingInput input)
        {
            bool result = false;
            try
            {
                //获取当前用户
                var currentUser = AsyncHelper.RunSync(this.GetCurrentUserAsync);
                var obj = _systemSettingRepository.Get(input.Id);
                obj.Parametername = input.Parametername;
                obj.Parametervalue = input.Parametervalue;
                obj.Status = input.Status;
                //var updateObj = Mapper.Map<FLowSet>(input);
                //_flowSetRepository.Update(updateObj);
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 获得系统参数信息
        /// </summary>
        /// <returns></returns>
        public SystemSettingDto GetSystemSettingId(int Id)
        {
            SystemSettingDto result = new SystemSettingDto();

            var obj = _systemSettingRepository.Get(Id);
            result = obj.MapTo<SystemSettingDto>();
            return result;
        }



    }
}
